/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the IncrementalParser package.
 *  
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.incparser.graphs;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class GraphOverlayAddEdge extends GraphOverlay implements GraphOverlayEdgeTypeInterface {
    final int layer;
    int innode;
    int outnode;
    int type;
    AbstractLocalGraph inNodeGraph = null;
    AbstractLocalGraph outNodeGraph = null;

    GraphOverlayAddEdge(AbstractGraph parentGraph, int layer, int innode, int outnode, int type) {
        // Call parent constructor and initialize variables
        super(parentGraph);
        this.layer = layer;
        setEdge(innode, outnode, type);
    }

    public void setEdge(final int innode, final int outnode, final int type) {
        // Clear all modifications to the parent graph and create initial local graphs
        if (inNodeGraph == null) {
            // Create new local graphs
            inNodeGraph = super._localGraphRW(innode, layer).clone();
            outNodeGraph = super._localGraphRW(outnode, layer).clone();
        } else {
            // Change existing local graphs
            clearModifications();
            int oldinnode = this.innode;
            int oldoutnode = this.outnode;
            int oldtype = this.type;
            this.inNodeGraph.removeInEdge(Helper.pack(oldoutnode, oldtype));
            this.outNodeGraph.removeOutEdge(Helper.pack(oldinnode, oldtype));
        }

        // Add new edge
        this.innode = innode;
        this.outnode = outnode;
        this.type = type;
        this.inNodeGraph.addInEdge(Helper.pack(outnode, type));
        this.outNodeGraph.addOutEdge(Helper.pack(innode, type));

        // Notify listeners
        baseGraph._notifyEdgeAdded(this, layer, innode, outnode, type);
    }

    public void setEdgeType(final int newtype) {
        int oldtype = this.type;
        inNodeGraph.removeInEdge(Helper.pack(innode, oldtype));
        inNodeGraph.addInEdge(Helper.pack(innode, newtype));
        outNodeGraph.removeOutEdge(Helper.pack(innode, oldtype));
        outNodeGraph.addOutEdge(Helper.pack(innode, newtype));
        baseGraph._notifyEdgeTypeChanged(this, layer, innode, outnode, oldtype, newtype);
    }

    @Override
    public LocalGraph localGraph(int node, int layer) {
        if (node == innode && layer == this.layer)
            return inNodeGraph;
        if (node == outnode && layer == this.layer)
            return outNodeGraph;
        return
            super.localGraph(node, layer);
    }

    @Override
    void _writeGraphChangesToBaseGraphPreprocess() {
        baseGraph.addEdge(layer, innode, outnode, type);
    }

    public void setBestEdgeType(int types) {
        double bestScore = Double.NEGATIVE_INFINITY;
        int bestType = 0;
        for (int type = 0; type < types; ++type) {
            setEdgeType(type);
            double newscore = score();
            if (newscore > bestScore) {
                bestScore = newscore;
                bestType = type;
            }
        }
        setEdgeType(bestType);
    }

    public int edgeType() {
        return type;
    }
}
